package xx.services.demo.rest;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.demo.DemoTabItem;
import xx.services.Passport;
import xx.services.demo.inter.ITabItemInterface;
import xx.services.util.Constants;
import xx.util.Arr;

/**
 * Demo TabItem服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/demo/tab/item/{item_id}")
 * @author Administrator
 * 
 * 2010-12-20
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/demo/tab/item/{item_id}")
public class TabItemRest extends Passport implements ITabItemInterface
{

    public TabItemRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 删除tab_item。<br>
     * 
     * @DELETE
     * 
     * @param itemId-@PathParam("item_id")
     *            Item ID，多个ID用“_”隔开
     * 
     * @return TabItem实体
     * @author chenxiang
     */
    @DELETE
    public String delete(@PathParam("item_id")
    String itemId)
    {
        try
        {
            int count = 0;
            long[] itemIdArr = Arr.toLongArray(itemId, "_");
            for (long id : itemIdArr)
            {
                DemoTabItem dti = new DemoTabItem(qr, owner);
                dti.setItemId(id);
                dti.delete();
                count++;
            }
            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 获取一个标签列item demo。<br>
     * 
     * @GET
     * 
     * @param itemId-@PathParam("item_id")
     *            Item ID
     * @return TabItem实体
     * @author chenxiang
     */
    @GET
    public String get(@PathParam("item_id")
    long itemId)
    {
        try
        {
            DemoTabItem dti = new DemoTabItem(qr, owner);
            dti.setItemId(itemId).load();
            return ok(dti);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 修改一个标签列item demo。<br>
     * 
     * @PUT
     * 
     * @param itemId-@PathParam("item_id")
     *            Item ID
     * @param title-@FormParam("title")
     *            item标题
     * @param container-@FormParam("container")
     *            item容器，一般是一个div，如：<div class="container">${container}</div>
     * @param demo-@FormParam("demo")
     *            Item实体
     * @param templet-@FormParam("templet")
     *            Item模板
     * @return 新TabItem实体
     * @author chenxiang
     */
    @PUT
    public String update(@PathParam("item_id")
    long itemId, @FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("demo")
    String demo, @FormParam("templet")
    String templet)
    {
        try
        {
            DemoTabItem dti = new DemoTabItem(qr, owner);
            dti.setItemId(itemId);
            StringBuilder condition = new StringBuilder();
            if (title != null && !"".equals(title.trim()))
            {
                condition.append("title,");
                dti.setTitle(title);
            }
            if (container != null && !"".equals(container.trim()))
            {
                condition.append("container,");
                dti.setContainer(container);
            }
            if (demo != null && !"".equals(demo.trim()))
            {
                condition.append("demo,");
                dti.setDemo(demo);
            }
            if (templet != null && !"".equals(templet.trim()))
            {
                condition.append("templet,");
                dti.setTemplet(templet);
            }

            if (condition.length() > 0)
            {
                dti.update(condition.toString());
            }
            return ok(dti);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }
}
